from selenium.webdriver.common.by import By

from common.utils import chromeDriver


class MyQuestion:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/repo/index"
        self.driver = chromeDriver.driver
        self.driver.get(self.url)

    #检查页面元素
    def MyQuestionCheckElement(self):
        #检查重置按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div:nth-child(1) > div > div:nth-child(1) > button")
        #检查查询按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div:nth-child(1) > div > div:nth-child(2) > button")
        #检查新建按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-pro-table-list-toolbar > div > div.ant-space.ant-space-horizontal.ant-space-align-center.ant-pro-table-list-toolbar-right > div:nth-child(1) > div > div:nth-child(1) > button")
        #检查导出按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-pro-table-list-toolbar > div > div.ant-space.ant-space-horizontal.ant-space-align-center.ant-pro-table-list-toolbar-right > div:nth-child(1) > div > div:nth-child(2) > button")
        #添加屏幕截图
        chromeDriver.getScreenShot()

    def MyQuestionFuncTest(self):
        #测试新建功能
        #点击新增按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-pro-table-list-toolbar > div > div.ant-space.ant-space-horizontal.ant-space-align-center.ant-pro-table-list-toolbar-right > div:nth-child(1) > div > div:nth-child(1) > button").click()

        #切换到点击新建的页面
        drawer = self.driver.find_element(By.CLASS_NAME, "ant-drawer-wrapper-body")

        #输入题库名称
        drawer.find_element(By.CSS_SELECTOR, "#name").send_keys("python")
        #输入题库分类
        drawer.find_element(By.CSS_SELECTOR, "#category").send_keys("脚本语言")
        #选择题库类型
        drawer.find_element(By.CSS_SELECTOR, "#mode").click()
        #添加屏幕截图
        chromeDriver.getScreenShot()

        select_option = drawer.find_element(By.CLASS_NAME, "rc-virtual-list-holder-inner")
        option = select_option.find_element(By.XPATH, f'.//div[@title="问卷"]')
        self.driver.execute_script("arguments[0].scrollIntoView;", option)
        self.driver.execute_script("arguments[0].click();", option)

        #点击确认按钮添加
        drawer.find_element(By.XPATH, "/html/body/div[4]/div/div[3]/div/div/div[3]/div/div/div[2]/button").click()
        #添加屏幕截图
        chromeDriver.getScreenShot()

        #测试搜索功能
        #在搜索框输入python
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("python")
        #点击查询按钮
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > div > div:nth-child(2) > button").click()
        #添加屏幕截图
        chromeDriver.getScreenShot()
        #验证是否查询成功，如果列表有输出则查询成功，否则查询失败
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div:nth-child(2) > div > div.ant-table-wrapper > div > div > div > div > div > table > tbody > tr.ant-table-row.ant-table-row-level-0 > td:nth-child(1)")

        #测试重置功能
        #点击重置功能
        self.driver.find_element(By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-pro-card.ant-pro-table-search.ant-pro-table-search-query-filter > form > div > div:nth-child(3) > div > div > div.ant-col.ant-form-item-control > div > div > div > div > div > div:nth-child(1) > button").click()
        #验证是否重置成功，如果输入框内没有内容则重置成功，否则重置失败
        text = self.driver.find_element(By.CSS_SELECTOR, "#name").text
        assert text == ""
        #添加屏幕截图
        chromeDriver.getScreenShot()
